/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.burlap.initializer;

import com.caucho.burlap.server.BurlapServlet;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.Set;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.burlap.initializer
 * @author:LiBo/Alex
 * @create-date:2020-04-06 22:00
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
@HandlesTypes({BurlapServlet.class})
public class BurlapServerContainerIntializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> c, ServletContext servletContext) throws ServletException {
        c.parallelStream().filter(Objects::nonNull).
                filter(param->!param.isInterface() && !Modifier.isAbstract(param.getModifiers())).forEach(
                param2-> {
                    ServletRegistration.Dynamic servletRegistration =
                            servletContext.addServlet(c.getClass().getSimpleName(), (Class<? extends Servlet>)param2);
                    String path = "/"+param2.getSimpleName().toLowerCase();
                    servletRegistration.addMapping(path);
                    log.info("加载Burlap远程服务对象类:{} - 映射关系:{}",param2.getSimpleName(),path);
                });
        }
}
